package org.orbroker

import org.orbroker.conv._
import org.orbroker.adapt.BrokerAdapter
import org.orbroker.callback.ExecutionCallback
import java.io.{ StringReader, StringWriter }
import freemarker.template._
import SQLStatement._

private[orbroker]class FreeMarkerStatement(
  id: Symbol,
  freemarkerSQL: Seq[String],
  trimSQL: Boolean,
  callback: ExecutionCallback,
  adapter: BrokerAdapter,
  config: Configuration)
    extends SQLStatement(id, callback, adapter) {

  private val template = new Template(id.toString, freemarkerSQL.mkString(EOL), config)

  override def statement(parms: Map[String, Any]) = {
    val context = toJavaMap(parms)
    val writer = new StringWriter
    template.process(context, writer)
    SQLStatement.parseSQL(id, writer, trimSQL, adapter)
  }
}
